package com.wxtl.action;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.wxtl.action.base.BaseAction;
import com.wxtl.exception.ServiceException;
import com.wxtl.util.DateUtil;

/**
 * 图片上传Action,针对于xheditor
 * 
 * @author hc24
 * 
 */
public class ImgAction extends BaseAction {
	private final static Log logger = LogFactory.getLog(ImgAction.class);

	// 属性用来封装上传的文件
	private File filedata;
	//ContentType属性用来封装上传文件的类型 
	private String filedataContentType;
	//FileName属性用来封装上传文件的文件名 
	private String filedataFileName;

	private String err;
	private String msg;

	public File getFiledata() {
		return filedata;
	}

	public void setFiledata(File filedata) {
		this.filedata = filedata;
	}

	public String getFiledataContentType() {
		return filedataContentType;
	}

	public void setFiledataContentType(String filedataContentType) {
		this.filedataContentType = filedataContentType;
	}

	public String getFiledataFileName() {
		return filedataFileName;
	}

	public void setFiledataFileName(String filedataFileName) {
		this.filedataFileName = filedataFileName;
	}

	public String getErr() {
		return err;
	}

	public void setErr(String err) {
		this.err = err;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String upload() throws Exception {

		HttpServletRequest request = ServletActionContext.getRequest();

		if (filedata == null || filedata.getName().equals("")) {
			throw new ServiceException("请选择上传的图片");
		}

		InputStream input = null;
		OutputStream output = null;
		String dirStr = ServletActionContext.getServletContext().getRealPath(
				"/upload/pic");
		File dir = new File(dirStr);
		if (!dir.exists())
			dir.mkdirs();
		// 确定服务器端写入文件的文件名
		String outFileName = DateUtil.getDateString(new Date())
				+ filedataFileName.substring(filedataFileName.lastIndexOf("."));
		String uploadfile = dirStr + "/" + outFileName;

		try {
			// 获得上传文件的输入流
			input = new FileInputStream(filedata);
			// 写入服务器的本地文件
			output = new BufferedOutputStream(new FileOutputStream(uploadfile));
			byte[] buffer = new byte[1024];
			int n;
			while ((n = input.read(buffer)) != -1)
				output.write(buffer, 0, n);
			
			
			this.msg=ServletActionContext.getServletContext().getContextPath()+"/upload/pic/"+outFileName;
			return SUCCESS;
		} catch (Exception e) {
			logger.equals(e);
			// throw new FileUploadException("上传文件失败："+e.getMessage());
			this.err="图片上传失败,请稍后重拾";
			return SUCCESS;
		} finally {
			// 必须在finally中关闭输入/输出流
			if (input != null)
				input.close();
			if (output != null)
				output.close();
		}

	}
}
